package com.wfy.orchestra.monitor

import android.util.Log
import com.wfy.orchestra.dispatch.ITask
import java.util.concurrent.ConcurrentHashMap

/**
 * @author : wangfengyuan
 * e-mail  : 446950883@qq.com
 * time    : 2025/3/20
 * desc    :
 * version : 1.0
 */
class DefaultPerfMonitor : PerformanceMonitor {
    private val submits = ConcurrentHashMap<String, Long>()
    private val starts = ConcurrentHashMap<String, Long>()
    override fun onTaskSubmit(task: ITask) {
        submits[task.id] = System.currentTimeMillis()
    }

    override fun onTaskStarted(task: ITask) {
        starts[task.id] = System.currentTimeMillis()
    }

    override fun onTaskFinished(task: ITask) {
        //实际执行耗时
        val execTime = System.currentTimeMillis() - starts.getValue(task.id)
        //提交到执行的时间差
        val queueTime = starts.getValue(task.id) - submits.getValue(task.id)
        Log.d("Perf", "${task.id} | QueueTime=${queueTime}ms | ExecTime=${execTime}ms")
    }
}